package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.CommonResult;
import com.itheima.mapper.FollowMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Follow;
import com.itheima.pojo.LikeAdd;
import com.itheima.pojo.User;
import com.itheima.service.UserLikeService;
import com.itheima.thread.UserThreadLocal;
import com.itheima.vo.LikeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangjiexin
 */
@Service
@Slf4j
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FollowMapper followMapper;

    @Override
    public Boolean add(LikeAdd dto) {
        // 1．入参校验
        if (dto == null || StringUtils.isEmpty(dto.getUserId())) {
            throw new RuntimeException("喜欢的id不能为空");
        }
        // 2．提取被喜欢者id
        String followId = dto.getUserId();
        // 3．提取我的id
        String userId = UserThreadLocal.get();
        // 4．检测被喜欢者是否存在（侧重于数据安全性的话，需要加这步)
        User followUser = userMapper.seleteById(followId);
        if (followUser == null) {
            throw new RuntimeException("被喜欢的用户不存在");
        }
        // 5．组装成pojo，调用insert方法写入到数据库中
        Follow follow = new Follow();
        follow.setId(IdUtil.getSnowflakeNextIdStr());
        follow.setUserId(userId);
        follow.setFollowId(followId);
        follow.setCreateTime(new Date());
        return followMapper.addOne(follow);
    }

    @Override
    public boolean del(String followId) {
        // 1．入参判断
        if (followId==null||StringUtils.isEmpty(followId)){
            throw new RuntimeException("取消喜欢的id不能为空");
        }
        // 2．被取消者id,就是上面传进来的id

        // 3．提取我的id
        String userId = UserThreadLocal.get();

        // 4．查询关系是否存在
        //查询用户id与喜欢的id,得到一个对象，然后if判断对象是否为空为null
      Follow follow =  followMapper.selectByUserIdAndFollowId(userId,followId);
            if (follow==null){
                log.info("关系不存在||有人刷接口");
               // throw new RuntimeException("喜欢关系不存在");
                //因为这边如果找不到这个对象的话，就没必要删除了，直接返回去就行了
                return true;
            }
        // 5．执行删除

        return followMapper.delByUserIdAndFollowId(userId,followId);
    }

    @Override
    public CommonResult list(String name, Integer type, Integer page, Integer pageSize) {
        //不做判空是因为注解会帮我们去验证，必填和非必填的字段
        //这边上一下公式
        page=(page-1)*pageSize;
        List<LikeVo> res=new ArrayList<>();
        //调用一下线程用户id
        String userId = UserThreadLocal.get();
        //声明总数的变量名
        Long total = null;
        //1．判断type类型，根据不同的type，调用不同的SQL语句
        if (type==1){
            //查互相关注
            res = followMapper.selectByType1(userId,name,page,pageSize);
           total=followMapper.selectCountByType1(userId,name);
        } else if (type==2) {
            // 查喜欢
            res = followMapper.selectByType2(userId,name,page,pageSize);
            total=followMapper.selectCountByType2(userId,name);
        } else if (type==3) {
            //查粉丝
            res = followMapper.selectByType3(userId,name,page,pageSize);
            total=followMapper.selectCountByType3(userId,name);
        }
        //查总数，搞个total 给前端统计个数去做分页用的

        //处理age字段

        return CommonResult.list(res,total);
    }
}
